using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Entity;
using Publicuse.Interface;
using Publicuse.Util;

namespace Publicuse.Cache
{
  public class UserAuthorCache
  {
    private static ConcurrentDictionary<string, List<User>> usercache { get; set; }
    private static ConcurrentDictionary<string, List<Role>> rolecache { get; set; }
    private static ConcurrentDictionary<string, List<UserRole>> userrolecache { get; set; }
    private static ConcurrentDictionary<string, List<Menu>> authorcache { get; set; }
    private static ConcurrentDictionary<string, List<URPermission>> urmcache { get; set; }
    private static ConcurrentDictionary<string, List<RoleResource>> rolerescache { get; set; }
    private static ConcurrentDictionary<string, List<Dictionaries>> dictionariescache { get; set; }


    static UserAuthorCache()
    {
      usercache = new ConcurrentDictionary<string, List<User>>();
      rolecache = new ConcurrentDictionary<string, List<Role>>();
      userrolecache = new ConcurrentDictionary<string, List<UserRole>>();
      authorcache = new ConcurrentDictionary<string, List<Menu>>();
      urmcache = new ConcurrentDictionary<string, List<URPermission>>();
      rolerescache = new ConcurrentDictionary<string, List<RoleResource>>();
      dictionariescache = new ConcurrentDictionary<string, List<Dictionaries>>();
    }

    #region 用户缓存
    public static void AddUsercache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        usercache.Clear();
      }
      var cachelist = GetUsercache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            usercache.Remove("user", out var listb);
            var table = TableAtributeUtil.GetTable<User>();
            var sql =
              $"select Id,uCode,uName,associateuon,associateuonuser from {table} where status<>-9;";
            var list = mySqlSugar.Query<User>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              usercache.TryAdd("user", list);
            }
          });
        }
        else
        {
          usercache.Remove("user", out var listb);
          var table = TableAtributeUtil.GetTable<User>();
          var sql =
            $"select Id,uCode,uName,associateuon,associateuonuser from {table} where status<>-9;";
          var list = mySqlSugar.Query<User>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            usercache.TryAdd("user", list);
          }
        }
      }
    }

    public static List<User> GetUsercache()
    {
      List<User> users = new List<User>();
      usercache.TryGetValue("user", out var list);
      if (list != null && list.Count > 0)
      {
        users = list;
      }
      return users;
    }
    #endregion

    #region 角色缓存
    public static void AddRolecache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        rolecache.Clear();
      }
      var cachelist = GetRolecache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            rolecache.Remove("role", out var listb);
            var table = TableAtributeUtil.GetTable<Role>();
            var sql = $"select Id,rName from {table} where status<>-9;";
            var list = mySqlSugar.Query<Role>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              rolecache.TryAdd("role", list);
            }
          });
        }
        else
        {
          rolecache.Remove("role", out var listb);
          var table = TableAtributeUtil.GetTable<Role>();
          var sql = $"select Id,rName from {table} where status<>-9;";
          var list = mySqlSugar.Query<Role>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            rolecache.TryAdd("role", list);
          }
        }
      }
    }

    public static List<Role> GetRolecache()
    {
      List<Role> roles = new List<Role>();
      rolecache.TryGetValue("role", out var list);
      if (list != null && list.Count > 0)
      {
        roles = list;
      }
      return roles;
    }
    #endregion

    #region 用户角色缓存
    public static void AddUserRolecache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        userrolecache.Clear();
      }
      var cachelist = GetUserRolecache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            userrolecache.Remove("userole", out var listb);
            var table = TableAtributeUtil.GetTable<UserRole>();
            var sql = $"select Id,uId,rids,type from {table} where status<>-9;";
            var list = mySqlSugar.Query<UserRole>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              userrolecache.TryAdd("userole", list);
            }
          });
        }
        else
        {
          userrolecache.Remove("userole", out var listb);
          var table = TableAtributeUtil.GetTable<UserRole>();
          var sql = $"select Id,uId,rids,type from {table} where status<>-9;";
          var list = mySqlSugar.Query<UserRole>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            userrolecache.TryAdd("userole", list);
          }
        }
      }
    }

    public static List<UserRole> GetUserRolecache()
    {
      List<UserRole> userroles = new List<UserRole>();
      userrolecache.TryGetValue("userole", out var list);
      if (list != null && list.Count > 0)
      {
        userroles = list;
      }
      return userroles;
    }
    #endregion

    #region 菜单资源缓存
    public static void AddAuthorcache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        authorcache.Clear();
      }
      var cachelist = GetAuthoritycache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            authorcache.Remove("authority", out var listb);
            var table = TableAtributeUtil.GetTable<Menu>();
            var sql = $"select Id,aName from {table} where status<>-9;";
            var list = mySqlSugar.Query<Menu>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              authorcache.TryAdd("authority", list);
            }
          });
        }
        else
        {
          authorcache.Remove("authority", out var listb);
          var table = TableAtributeUtil.GetTable<Menu>();
          var sql = $"select Id,aName from {table} where status<>-9;";
          var list = mySqlSugar.Query<Menu>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            authorcache.TryAdd("authority", list);
          }
        }
      }
    }

    public static List<Menu> GetAuthoritycache()
    {
      List<Menu> users = new List<Menu>();
      authorcache.TryGetValue("authority", out var list);
      if (list != null && list.Count > 0)
      {
        users = list;
      }
      return users;
    }
    #endregion

    #region 用户角色权限资源缓存
    public static void AddURMcache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        urmcache.Clear();
      }
      var cachelist = GetURMcache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            urmcache.Remove("userroleres", out var listb);
            var table = TableAtributeUtil.GetTable<URPermission>();
            var sql = $"select Id,uId,rId,aids from {table} where status<>-9;";
            var list = mySqlSugar.Query<URPermission>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              urmcache.TryAdd("userroleres", list);
            }
          });
        }
        else
        {
          urmcache.Remove("userroleres", out var listb);
          var table = TableAtributeUtil.GetTable<URPermission>();
          var sql = $"select Id,uId,rId,aids from {table} where status<>-9;";
          var list = mySqlSugar.Query<URPermission>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            urmcache.TryAdd("userroleres", list);
          }
        }
      }
    }

    public static List<URPermission> GetURMcache()
    {
      List<URPermission> urms = new List<URPermission>();
      urmcache.TryGetValue("userroleres", out var list);
      if (list != null && list.Count > 0)
      {
        urms = list;
      }
      return urms;
    }
    #endregion

    #region 角色资源缓存
    public static void AddRoleRescache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        rolerescache.Clear();
      }
      var cachelist = GetRoleRescache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            rolerescache.Remove("roleres", out var listb);
            var table = TableAtributeUtil.GetTable<RoleResource>();
            var sql = $"select Id,rId,aIds from {table} where status<>-9;";
            var list = mySqlSugar.Query<RoleResource>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              rolerescache.TryAdd("roleres", list);
            }
          });
        }
        else
        {
          rolerescache.Remove("roleres", out var listb);
          var table = TableAtributeUtil.GetTable<RoleResource>();
          var sql = $"select Id,rId,aIds from {table} where status<>-9;";
          var list = mySqlSugar.Query<RoleResource>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            rolerescache.TryAdd("roleres", list);
          }
        }
      }
    }

    public static List<RoleResource> GetRoleRescache()
    {
      List<RoleResource> users = new List<RoleResource>();
      rolerescache.TryGetValue("roleres", out var list);
      if (list != null && list.Count > 0)
      {
        users = list;
      }
      return users;
    }
    #endregion


    #region 数据字典缓存
    public static void AddDictionariescache(
      IMySqlSugarService mySqlSugar,
      bool istask = true,
      bool isreset = false
    )
    {
      if (isreset)
      {
        dictionariescache.Clear();
      }
      var cachelist = GetDictionariescache();
      if (cachelist.Count == 0)
      {
        if (istask)
        {
          Task.Factory.StartNew(() =>
          {
            dictionariescache.Remove("dictionaries", out var listb);
            var table = TableAtributeUtil.GetTable<Dictionaries>();
            var sql = $"select Id,dName from {table} where status<>-9;";
            var list = mySqlSugar.Query<Dictionaries>(sql, null, out var msg);
            if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
            {
              dictionariescache.TryAdd("dictionaries", list);
            }
          });
        }
        else
        {
          dictionariescache.Remove("dictionaries", out var listb);
          var table = TableAtributeUtil.GetTable<Dictionaries>();
          var sql = $"select Id,dName from {table} where status<>-9;";
          var list = mySqlSugar.Query<Dictionaries>(sql, null, out var msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            dictionariescache.TryAdd("dictionaries", list);
          }
        }
      }
    }

    public static List<Dictionaries> GetDictionariescache()
    {
      List<Dictionaries> dictionaries = new List<Dictionaries>();
      dictionariescache.TryGetValue("dictionaries", out var list);
      if (list != null && list.Count > 0)
      {
        dictionaries = list;
      }
      return dictionaries;
    }
    #endregion
  }
}
